package laboratoryQ.ui.draw;

import java.awt.Color;
import java.awt.Graphics;

import laboratoryQ.flatness.DiagramData;
import laboratoryQ.flatness.FlatnessUtils;
import laboratoryQ.flatness.ICoordinateTransformer;
import laboratoryQ.ui.draw.style.ColorProperty;
import laboratoryQ.ui.draw.style.DiagramItemColorProperty;
import laboratoryQ.ui.draw.style.IStylizer;
import laboratoryQ.ui.draw.style.Style;

public class DiagramDrawer implements IDrawer {
	private DiagramData diagram;
	private DiagramData buffer = new DiagramData();
	private ICoordinateTransformer transformer;
	private IDrawStrategy drawStrategy = IDrawStrategy.DefaultDrawStrategy.INSTANCE;
	private Style style = new Style();
	private ColorProperty colorProperty;
	
	public DiagramDrawer(DiagramData diagram, ICoordinateTransformer transformer, int position) {
		this.diagram = diagram;
		int n = diagram.x.length;
		buffer.x = new double[n];
		buffer.y = new double[n];
		this.transformer = transformer;
		colorProperty = position > -1 ? new DiagramItemColorProperty(position) : new ColorProperty();
		style.listPropertices().add(colorProperty);
	}

	@Override
	public void draw(Graphics g, IStylizer stylizer) {
		stylizer.fillStyle(style);
		
		Color color = (Color) colorProperty.getValue();
		g.setColor(color);
		int n = diagram.x.length;
		FlatnessUtils.doTransform(
				diagram.x, 
				diagram.y,
				buffer.x, 
				buffer.y,
				transformer, 
				g
		);
		for (int i = 1; i < n; i++) {
			int xp = (int) buffer.x[i - 1];
			int yp = (int) buffer.y[i - 1];
			int xc = (int) buffer.x[i];
			int yc = (int) buffer.y[i];
			drawStrategy.drawLine(g, xp, yp, xc, yc);
		}
	}
}
